﻿using System;
using System.Diagnostics;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using ColumnModuleAndEdit.DataDefine;
using ColumnModuleAndEdit.Forms;
using YJKCodeBase.WinForm;
using YJKView;

namespace ColumnModuleAndEdit.ColumnModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	internal class CmdColumnInsert : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (this.m_doc.ActiveView is ViewPlan && this.m_doc.ActiveView.SketchPlane == null)
			{
				Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(this.m_doc.ActiveView.Origin, this.m_doc.ActiveView.RightDirection, this.m_doc.ActiveView.UpDirection);
				Transaction transaction = new Transaction(this.m_doc, "SketchPlane");
				transaction.Start();
				SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(this.m_doc, plane);
				this.m_doc.ActiveView.SketchPlane = sketchPlane;
				transaction.Commit();
			}
			IntPtr intPtr = (IntPtr)0;
			Process currentProcess = Process.GetCurrentProcess();
			intPtr = currentProcess.MainWindowHandle;
			WindowHandle owner = new WindowHandle(intPtr);
			AssistFunc.GetCurrentViewLevel(this.m_doc);
			try
			{
				using (ColumnInsertDlg columnInsertDlg = new ColumnInsertDlg(this.m_uiDoc, intPtr))
				{
					columnInsertDlg.Show(owner);
					ColumnInsertManage columnInsertManage = new ColumnInsertManage(cmdData);
					while (!columnInsertDlg.m_bIsDlgClosed)
					{
						if (!columnInsertManage.ColumnMainOperator(columnInsertDlg) && !columnInsertDlg.m_bIsLevelDlg)
						{
							columnInsertDlg.Close();
						}
						columnInsertDlg.m_bIsLevelDlg = false;
					}
				}
			}
			catch (Exception)
			{
			}
			return base.YJKExcute(cmdData, ref message, elements);
		}
	}
}
